package com.yubest.satoken.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: hweiyu
 * @Date: 2023/10/10 10:56
 */
@RestController
@RequestMapping("/user/")
public class UserController {

    @PostMapping("login")
    public String login(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("admin".equals(username) && "123456".equals(password)) {
            StpUtil.login("admin");
            return "登录成功";
        } else if("test".equals(username) && "123456".equals(password)) {
            StpUtil.login("test");
            return "登录成功";
        }
        return "登录失败";
    }

    @GetMapping("logout")
    public String logout() {
        Object loginId = StpUtil.getLoginId();
        StpUtil.logout();
        return "退出成功：" + loginId;
    }
}

